﻿using Kendo.Mvc.UI;
using Library.AppWeb;
using MPR.Poker.Model.SYS;
using MPR.Poker.DALEntity.SYS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kendo.Mvc.Extensions;

namespace PokerLife.Controllers.SYS
{
    public class DisplayConfigController : Controller
    {
        //
        // GET: /DisplayConfig/

        public ActionResult Index(string TableName, string ColumnList)
        {
            ViewBag.ColumnLists = ColumnList;
            ViewBag.TableName = TableName;
            return View();
        }

        public ActionResult DisplayConfig_Read([DataSourceRequest] DataSourceRequest request, string TableName, string LanguageID, string ColumnLists)
        {
            int LanguageIDSys = int.Parse(LanguageID);

            if (User != null)
            {
                DAL_DisplayConfig db = new DAL_DisplayConfig();
                List<SYS_DisplayConfig> lst = new List<SYS_DisplayConfig>();
                ResultMessage result = db.SelectDisplayConfigList(ref lst, TableName, LanguageIDSys);

                if (ColumnLists.Trim().Length > 0)
                    ColumnLists = ColumnLists.Trim().Substring(0, ColumnLists.Trim().Length - 1);
                string[] lstcol = ColumnLists.Split(',');

                List<string> ColNew = lstcol.Except(lst.Select(p => p.ColumnName)).ToList();
                for (int i = 0; i < ColNew.Count; i++)
                {
                    SYS_DisplayConfig dis = new SYS_DisplayConfig();
                    dis.ColumnName = ColNew[i].Trim();
                    dis.OrderIndex = lst.Count + 1;
                    dis.IDSys = lst.Count + 1;
                    dis.IsNewCol = true;
                    dis.HeaderAlign = "center";
                    dis.DataAlign = "left";
                    dis.ColumnWidth = 50;
                    lst.Add(dis);
                }


                return Json(lst.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
            }
            return Json(null);
        }

        public ActionResult SaveChange(IEnumerable<SYS_DisplayConfig> configs)
        {
            DAL_DisplayConfig db = new DAL_DisplayConfig();

            ResultMessage result = db.UpdateDisplayConfig(configs.ToList());

            return Json((!result.IsError).ToString().ToLower());
            //return Json("");
        }

    }
}
